import { Http,Jsonp } from '@angular/http';
import { Injectable } from '@angular/core';
import { ConfigProvider } from '../config/config';



/*
  Generated class for the HttpServicesProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class HttpServicesProvider {

  constructor(public http: Http,public jsonp:Jsonp,public config:ConfigProvider) {
    
  }

  // 请求数据
  requestData(apiUrl,callback){
    let api:string = ''
    // 判断传入的接口是否有参数，
    if(apiUrl.indexOf('?') == -1){
      // 没有参数情况
      api = this.config.apiUrl+apiUrl+'?callback=JSONP_CALLBACK'
    }else{
      // 有参数的情况
      api = this.config.apiUrl+apiUrl+'&callback=JSONP_CALLBACK'
    }
    this.jsonp.get(api).subscribe(data=>{
      callback(data['_body'])//回调函数
    },err=>{
      console.log(err)
    })
  }

}
